package com.douwong.student.task;

import android.os.AsyncTask;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.douwong.student.application.BaseApplication;
import com.douwong.student.contant.NetConfig;
import com.douwong.student.interfaces.WorkResultNotify;
import com.douwong.student.model.HandWriterData;
import com.douwong.student.model.RequestAction;
import com.douwong.student.utils.LogUtils;
import com.douwong.student.utils.NetUtils;
import com.douwong.student.utils.ToastUtils;
import com.douwong.student.utils.UIUtils;

import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
 * @autor yichao
 * @time 2017/6/1411:13
 * @des 手写互动
 */
public class HanderWriterListTask extends AsyncTask<String,Integer,Integer> {
    private static final String TAG = "HanderWriterListTask";
    public static int NO_DATA = 0;// 没有新的数据
    public static int REQUESTOK = 1;// 加载更多
    public static int NO_NETWOK = -1;// 没有网络
    private RequestAction mAction;
    private boolean isSearch;
    private BaseApplication mApplication;
    private WorkResultNotify<HandWriterData.DataEntity.ItemsEntity> mResultNotify;
    private  String   key;
    private int resultCode;
    private List<HandWriterData.DataEntity.ItemsEntity> mItems;

    public HanderWriterListTask(RequestAction action, boolean isSearch, BaseApplication application, WorkResultNotify<HandWriterData.DataEntity.ItemsEntity> resultNotify) {
       this. mAction = action;
        this.isSearch = isSearch;
        mApplication = application;
        this.mResultNotify = resultNotify;
    }

    @Override
    protected Integer doInBackground(String... params) {
        key = BaseApplication.sUserBean.getUserbean().getEuNumber() + "_handerwriter"
                + params[2];
        if (RequestAction.INIT == mAction) {
            if (mApplication.readObject(key) != null) {
                mItems = (List<HandWriterData.DataEntity.ItemsEntity>) mApplication.readObject(key);
                return REQUESTOK;
            } else if (NetUtils.NetwrokIsUseful(UIUtils.getContext())) {
                resultCode = accessNetworkRequestData(params);
            } else {
                resultCode = NO_NETWOK;
            }
        } else if (NetUtils.NetwrokIsUseful(UIUtils.getContext())) {
            resultCode = accessNetworkRequestData(params);
        } else {
            resultCode = NO_NETWOK;
        }

        return resultCode;
    }

    private int accessNetworkRequestData(String... params) {
        String requestionBody="";
        try {
        //mSubjectId,mStartTime,currentPage+""
        List<BasicNameValuePair> basicNameValuePairs=new ArrayList<>();
        if (params[0]!=null){
          basicNameValuePairs.add(new BasicNameValuePair("subjectid",params[0]));
        }else {
            basicNameValuePairs.add(new BasicNameValuePair("subjectid","0"));
        }
        if (params[2]!=null){
          basicNameValuePairs.add(new BasicNameValuePair("create_time",params[1]));
        }else {
            basicNameValuePairs.add(new BasicNameValuePair("create_time",""));
        }
          basicNameValuePairs.add(new BasicNameValuePair("pagenum","30"));
            basicNameValuePairs.add(new BasicNameValuePair("class_id", BaseApplication.sUserBean
                    .getPersonalbean().getClassId()+""));
        requestionBody = NetUtils.httpPost(NetConfig.HAND_WRITER_EXCHANGE_LIST + params[2], basicNameValuePairs);
            LogUtils.i(TAG, requestionBody);
            if (requestionBody!=null ){
                HandWriterData sc = (HandWriterData) JSON
                        .parseObject(requestionBody,
                                new TypeReference<HandWriterData>() {});
                LogUtils.i(TAG, requestionBody);
                if (sc!=null){
                    if (sc.isFlag()){
                        if (sc.getData().getItems()==null || sc.getData().getItems().size()==0){
                            return NO_DATA;
                        }
                        mItems = sc.getData().getItems();
                        mApplication.saveObject(mItems,key);
                        return REQUESTOK;
                    }else {
                        ToastUtils.makeText(UIUtils.getContext(),sc.getMsg());
                        return NO_DATA;
                    }
                }


            }
            LogUtils.i(TAG, requestionBody);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return NO_DATA;


    }


    @Override
    protected void onPostExecute(Integer integer) {
        super.onPostExecute(integer);
        if (mResultNotify!=null){
            mResultNotify.notify(integer,mItems);
        }
    }
}
